<?php
/**
 * Created by PhpStorm.
 * User: Kau
 * Date: 2017/12/30
 * Time: 21:46
 */
namespace app\modules\controllers;

use app\modules\models\Admin;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;

class ManageController extends Controller {
    /**
     * 验证邮件发送修改密码操作
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionMailchangepass() {
        $this->layout = false;
        $time = Yii::$app->request->get("timestamp");
        $adminuser = Yii::$app->request->get('adminuser');
        $token = Yii::$app->request->get('token');
        $model = new Admin();
        $mytoken = $model->createToken($adminuser, $time);
        if ($token != $mytoken) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        if (time() - $time > 600) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changePass($post)) {
                Yii::$app->session->setFlash('info','密码修改成功！');
            }
        }
        $model->adminuser = $adminuser;
        return $this->render("mailchangepass", ['model' => $model]);
    }

    /**
     * 展示所有的管理员
     * @return string
     */
    public function actionManagers(){
        $this->layout = 'layout1';
        $model = Admin::find();
        $count = $model->count();
        $pageSize = Yii::$app->params['pageSize']['manage'];
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $managers = $model->offset($pager->offset)->limit($pager->limit)->all();
        return $this->render('managers',['managers' => $managers, 'pager' => $pager]);
    }

    /**
     * 添加管理员
     * @return string
     */
    public function actionReg() {
        $this->layout = 'layout1';
        $model = new Admin();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->reg($post)) {
                Yii::$app->session->setFlash('info','添加成功');
            } else {
                Yii::$app->session->setFlash('info','添加失败');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('reg',['model' => $model]);
    }

    /**
     * 删除管理员操作
     */
    public function actionDel() {
        $adminid = (int)Yii::$app->request->get('adminid');
        if (empty($adminid)) {
            $this->redirect(['manage/managers']);
        }
        $model = new Admin();
        if ($model->deleteAll('adminid = :id', [':id' => $adminid])) {
            Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['manage/managers']);
        }
    }

    /**
     * 修改管理员邮箱
     * @return string
     */
    public function actionChangeemail() {
        $this->layout = 'layout1';
        $model = Admin::find()->where('adminuser = :user',[':user' => Yii::$app->session['admin']['adminuser']])->one();

        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changeemail($post)) {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }
        $model->adminpass = '';
        return $this->render('changeemail', ['model' => $model]);
    }

    /**
     * 修改管理员密码
     * @return string
     */
    public function actionChangepass() {
        $this->layout = 'layout1';
        $model = Admin::find()->where('adminuser = :user',[':user' => Yii::$app->session['admin']['adminuser']])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changePass($post)) {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('changepass', ['model' => $model]);
    }


}